package com.hexb.llk.arithmetic
{
	import com.hexb.llk.data.Element;
	import com.hexb.llk.utils.Constant;
	
	import starling.textures.ConcreteTexture;
	import com.hexb.llk.data.ElementPoint;

	public class BaseConnectArithmetic
	{
		private var path:Array = new Array(4);//用于存放连接点的数组
		private var canCrossFun:Function = null ;//用于调用算法对象传入是否可以在直线上通过的函数
		
		public function hasConnectPath(p1:ElementPoint, p2:ElementPoint, snapshot:Array):Boolean{
			clearPath();
			if(inSameLine(p1,p2,snapshot)){
				return true ;
			}
			clearPath();
			if(oneCorner(p1,p2,snapshot)){
				return true ;
			}
			clearPath();
			if(towCorner(p1,p2,snapshot)){
				return true;
			}
			clearPath();
			return false ;
		}
		
		public function getConnectPath():Array{
			return path ;
		}
		
		public function setCanCrossFun(fun:Function):void{
			this.canCrossFun = fun ;
		}
		
		public function hasConnectElement(elements:Array,snapshot:Array):Boolean {
			if(null!=elements && elements.length>0){
				var index:int = 0 ;
				for(var i:int = 0;i<elements.length;i++){
					for(var j:int=0;j<elements.length;j++){
						if(i==j){
							continue; 
						}
						if(hasConnectPath(elements[i]["location"],elements[j]["location"],snapshot)){
							return true ;
						}
					}
				}
			}
			return false ;
		}
		
		
		/*
		 * 判断是否可以两折相连
		 * */
		private function towCorner(p1:ElementPoint, p2:ElementPoint, snapshot:Array):Boolean{
			var result:Boolean = false;
			if(p2.col <= p1.col){
				result = towCornerRightAndLeft(p2,p1,snapshot,1) ;
				if(result){
					return true ;
				}
			}else{
				result = towCornerRightAndLeft(p1,p2,snapshot,1);
				if(result){
					return true ;
				}
			}
			if(p2.col >= p1.col){
				result = towCornerRightAndLeft(p2,p1,snapshot,-1);
				if(result){
					return true ;
				}
			}else{
				result = towCornerRightAndLeft(p1,p2,snapshot,-1) ;
				if(result){
					return true ;
				}
			}
			if(p2.row <= p1.row){
				result = towCornerTopAndBottom(p1,p2,snapshot,1) ;
				if(result){
					return true ;
				}
			}else{
				result = towCornerTopAndBottom(p2,p1,snapshot,1) ;
				if(result){
					return true ;
				}
			}
			if(p2.row >= p1.row){
				result = towCornerTopAndBottom(p2,p1,snapshot,-1) ;
				if(result){
					return true ;
				}
			}else{
				result = towCornerTopAndBottom(p1,p2,snapshot,-1) ;
				if(result){
					return true ;
				}
			}
			return false;
		}
		
		/*传入的p1,p2相应的列已经为排列好大小顺序的列*/
		private function towCornerRightAndLeft(p1:ElementPoint,p2:ElementPoint,snapshot:Array,direction:int):Boolean{
			var colNum:int = (snapshot[0] as Array).length;
			var i:int ;
			var tempPointA:ElementPoint ;
			var tempPointB:ElementPoint ;
			var temp:Boolean ;
			for(i= p1.col+direction ;i<colNum&&i>=0 ;i+=direction){
				if(!canCross(snapshot[p1.row][i]) || !canCrossFun(snapshot[p2.row][i]) || inSameLine(new ElementPoint(p2.row,i),p2,snapshot)==false){
					return false ;
				}
				tempPointA = new ElementPoint(p1.row,i);
				tempPointB = new ElementPoint(p2.row,i);
				temp = inSameLine(tempPointA,tempPointB,snapshot) ;
				if(temp==true){
					path[0] = p1 ;
					path[1] = tempPointA ;
					path[2] = tempPointB ;
					path[3] = p2 ;
					return true ;
				}
			}
			return false;
		}
		
		/*传入的p1,p2相应的列已经为排列好大小顺序的列*/
		private function towCornerTopAndBottom(p1:ElementPoint,p2:ElementPoint,snapshot:Array,direction:int):Boolean{
			var rowNum:int = snapshot.length;
			var i:int ;
			var tempPointA:ElementPoint ;
			var tempPointB:ElementPoint ;
			var temp:Boolean ;
			for(i=p1.row+direction;i<rowNum && i>=0 ;i+=direction){
				if(!canCrossFun(snapshot[i][p1.col]) || !canCrossFun(snapshot[i][p2.col]) || inSameLine(new ElementPoint(i,p2.col),p2,snapshot)==false){
					return false ;
				}
				tempPointA = new ElementPoint(i,p1.col);
				tempPointB = new ElementPoint(i,p2.col);
				temp = inSameLine(tempPointA,tempPointB,snapshot) ;
				if(temp==true){
					path[0] = p1 ;
					path[1] = tempPointA ;
					path[2] = tempPointB ;
					path[3] = p2 ;
					return true ;
				}
			}
			return false;
		}
		
		
		/*
		 * 判断是否可以一折相连
		 * */
		private function oneCorner(p1:ElementPoint, p2:ElementPoint, snapshot:Array):Boolean{
			//找出两个中间点
			var tempPoint4One:ElementPoint = new ElementPoint(p1.row,p2.col);
			var tempPoint4Two:ElementPoint = new ElementPoint(p2.row,p1.col);
			
			if(inSameLine(p1,tempPoint4One,snapshot) && inSameLine(tempPoint4One,p2,snapshot)){
				path[0] = p1 ;
				path[1] = tempPoint4One ;
				path[3] = p2 ;
				return true ;
			}
			if(inSameLine(p1,tempPoint4Two,snapshot) && inSameLine(tempPoint4Two,p2,snapshot)){
				path[0] = p1 ;
				path[1] = tempPoint4Two ;
				path[3] = p2 ;
				return true ;
			}
			path[1] = null ;
			return false ;
		}

		/*
		 * 判断是否在同一条线上
		 **/
		private function inSameLine(p1:ElementPoint,p2:ElementPoint,sanpshot:Array):Boolean{
			if(p1.col == p2.col){
				path[0] = p1 ;
				path[3] = p2 ;
				return sameColCanConnect(p1,p2,sanpshot);
			}
			if(p1.row == p2.row){
				path[0] = p1 ;
				path[3] = p2 ;
				return sameRowCanConnect(p1,p2,sanpshot);
			}
			return false;
		}
		
		
		/*
		 * 直线-同一行
		 **/
		private function sameRowCanConnect(p1:ElementPoint,p2:ElementPoint,sanpshot:Array):Boolean{
			var startCol:int = p1.col < p2.col ? p1.col : p2.col ;
			var endCol:int = p1.col > p2.col ? p1.col : p2.col;
			var row:int = p1.row ;
			
			var i:int ;
			for(i = startCol+1 ;i<endCol ;i++){
				if(!canCrossFun(sanpshot[row][i])){
					return false;
				}
			}
			return true ;
		}
		
		/*
		 * 直线-同一列
		 **/
		private function sameColCanConnect(p1:ElementPoint,p2:ElementPoint,sanpshot:Array):Boolean{
			var startRow:int = p1.row < p2.row ? p1.row : p2.row ;
			var endRow:int = p1.row > p2.row ? p1.row : p2.row;
			var col:int = p1.col ;
			var i:int ;
			for(i = startRow+1 ;i<endRow ;i++){
				if(!canCrossFun(sanpshot[i][col])){
					return false ;
				}
			}
			return true ;
		}
		
		private function clearPath():void{
			path[0] = null ;
			path[1] = null ;
			path[2] = null ;
			path[3] = null ;
		}
		
		public function BaseConnectArithmetic(){
			canCrossFun = this.canCross ;
		}
		
		/*判断是否在当前布局下直线上通过函数默认实现*/
		private function canCross(value:int):Boolean{
			if(value==Constant.HAVE_PIECE){
				return false ;
			}
		} 
		
	}
}